package com.ftg.learn.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.mall.dto.PmsProductParam;
import com.ftg.learn.mall.dto.PmsProductQueryParam;
import com.ftg.learn.mall.entity.PmsProduct;
import com.ftg.learn.mall.service.IPmsProductService;
import com.ftg.learn.util.ResEntity;
import com.ftg.learn.util.ResPageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/mall/pmsProduct")
public class PmsProductController {

    @Autowired
    IPmsProductService iPmsProductService;


    /**
     * 商品添加
     */
    @PostMapping("/insert")
    public ResEntity insert(@RequestBody PmsProductParam pmsProductParam){
        return iPmsProductService.insert(pmsProductParam) ? new ResEntity() : new ResEntity(-1,"failure");
    }

    /**
     * 根据商品id获取当前商品编辑信息
     * @param pid
     * @return
     */
    @GetMapping("/findUpdateInfo/{pid}")
    public ResEntity findUpdateInfo(@PathVariable("pid") Long pid){
        return new ResEntity(0,"success",iPmsProductService.getUpdateInfo(pid));
    }

    /**
     * 更新商品信息
     * @param pmsProductParam
     * @return
     */
    @PutMapping("/update")
    public ResEntity update(@RequestBody PmsProductParam pmsProductParam){
        return iPmsProductService.update(pmsProductParam) ? new ResEntity() : new ResEntity(-1,"failure");
    }


    /**
     *  分页查询商品
     * @param pmsProductQueryParam
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/findList")
    public ResEntity findList(@RequestBody PmsProductQueryParam pmsProductQueryParam,
                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "limit", defaultValue = "5") Integer limit
                              ){
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(pmsProductQueryParam.getProductSn())){
            queryWrapper.eq("product_sn",pmsProductQueryParam.getProductSn());
        }
        if (Objects.nonNull(pmsProductQueryParam.getProductCategoryId())){
            queryWrapper.eq("product_category_id",pmsProductQueryParam.getProductCategoryId());
        }
        if (Objects.nonNull(pmsProductQueryParam.getBrandId())){
            queryWrapper.eq("brand_id",pmsProductQueryParam.getBrandId());
        }
        if (Objects.nonNull(pmsProductQueryParam.getKeyword())){
            queryWrapper.like("product_name",pmsProductQueryParam.getKeyword());
        }
        if (Objects.nonNull(pmsProductQueryParam.getPublishStatus())){
            queryWrapper.eq("publish_status",pmsProductQueryParam.getPublishStatus());
        }
        if (Objects.nonNull(pmsProductQueryParam.getVerifyStatus())){
            queryWrapper.eq("verify_status",pmsProductQueryParam.getVerifyStatus());
        }
        IPage<PmsProduct> ip = new Page<>(page,limit);
        iPmsProductService.page(ip,queryWrapper);
        return new ResPageEntity(ip);
    }

    /**
     * 批量修改审核状态
     * @param ids
     * @param publishStatus
     * @return
     */
    @PutMapping("/updateVerifyStatus")
    public ResEntity updateVerifyStatus(@RequestParam List<Long> ids,@RequestParam long publishStatus){
        int cishu = iPmsProductService.updateVerifyStatus(ids,publishStatus);
        if (cishu>0){
            return new ResEntity(0,"成功修改"+cishu+"条");
        }else {
            return new ResEntity(-1, "修改失败");
        }
    }

    /**
     * 批量上下架商品
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PutMapping("/updatePublishStatus")
    public ResEntity updatePublishStatus(@RequestParam List<Long> ids,@RequestParam long recommendStatus){
        int cishu = iPmsProductService.updatePublishStatus(ids,recommendStatus);
        if (cishu>0){
            return new ResEntity(0,"成功修改"+cishu+"条");
        }else {
            return new ResEntity(-1, "修改失败");
        }
    }


    /**
     * 批量修改删除状态
     * @param ids
     * @param deleteStatus
     * @return
     */
    @PutMapping("/updateDeleteStatus")
    public ResEntity updateDeleteStatus(@RequestParam List<Long> ids,@RequestParam long deleteStatus){
        int cishu = iPmsProductService.updateDeleteStatus(ids,deleteStatus);
        if (cishu>0){
            return new ResEntity(0,"成功修改"+cishu+"条");
        }else {
            return new ResEntity(-1, "修改失败");
        }
    }





}
